package com.hebeiwangxiaozhang.product.controller;

import com.hebeiwangxiaozhang.pojo.CommodityEsBody;
import com.hebeiwangxiaozhang.pojo.CommodityOnSaleBody;
import com.hebeiwangxiaozhang.product.service.ProductService;
import com.hebeiwangxiaozhang.response.CommonResponse;
import com.hebeiwangxiaozhang.response.ResponseCode;
import com.hebeiwangxiaozhang.response.ResponseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/product")
public class ProductController {

    @Autowired
    private ProductService productService;

    @RequestMapping("/on-sale")
    public CommonResponse onSaleToEs(@RequestBody CommodityOnSaleBody onSaleBody) {
        List<CommodityEsBody> esBodies = onSaleBody.convertToEsBody();
        List<String> failedBulkList = productService.onSaleToEs(esBodies);
        if(failedBulkList.isEmpty()) {
            return ResponseUtils.okResponse(null);
        }
        return ResponseUtils.failResponse(ResponseCode.BAD_ESBULK_REQUEST.getCode(), failedBulkList, ResponseCode.BAD_ESBULK_REQUEST.getMessage());
    }
}
